package org.springblade.modules.admin.service.parts.impl.strategy;

import lombok.extern.slf4j.Slf4j;
import org.springblade.core.log.exception.ServiceException;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.core.tool.api.R;
import org.springblade.modules.admin.entity.BusinessApprove;
import org.springblade.modules.admin.enums.ApproveStatusEnum;
import org.springblade.modules.admin.enums.ApproveTypeEnum;
import org.springblade.modules.admin.enums.TaskStatusEnum;
import org.springblade.modules.admin.service.approve.strategy.ApproveBusinessFlow;
import org.springblade.modules.admin.service.approve.strategy.ApproveBusinessStrategy;
import org.springblade.modules.admin.service.parts.AbstractOrderStateStrategy;
import org.springblade.modules.admin.vo.BusinessApproveVO;
import org.springblade.modules.admin.vo.parts.PartsExecuteVO;
import org.springframework.stereotype.Service;

/**
 * @Description 待报价策略实现 -> 对应状态2
 * @Author ysq
 * @Date 2024/05/16
 **/
@Slf4j
@Service(value = "offerOrderStrategy")
public class OfferOrderStrategyImpl extends AbstractOrderStateStrategy {

	@Override
	public Object handle(PartsExecuteVO executeParams) {
		BusinessApprove businessApprove = businessApproveService.getByWorkOrderId(executeParams.getPlanId());
		if (businessApprove == null) {
			log.error("审批流程不存在!, 工单id：[{}]", executeParams.getPlanId());
			return R.status(false);
		}
		if (businessApprove.getApproveStatus() != ApproveStatusEnum.PENDING.getId()) {
			log.error("当前流程状态为：{},不能进行审批, 工单id：[{}]",  ApproveStatusEnum.getName(businessApprove.getApproveStatus()), executeParams.getPlanId());
			return R.status(false);
		}
		return super.handle(executeParams);
	}

	@Override
	public void after(PartsExecuteVO executeParams) {
		// 插入任务追踪日志
		maintainMalfunctionLogService.insertWorkLog(executeParams.getPlanId(),
			TaskStatusEnum.valueOf(executeParams.getEvent()).getName(),
			TaskStatusEnum.valueOf(executeParams.getEvent()).getStatus(),
			null,
			AuthUtil.getUserId(),
			"报价员");
	}
}
